package com.elvish.efast.modules.sys.controller;

import com.elvish.efast.common.anno.NeedPage;
import com.elvish.efast.common.consts.Const;
import com.elvish.efast.common.entity.Page;
import com.elvish.efast.common.entity.Result;
import com.elvish.efast.common.entity.TableResponse;
import com.elvish.efast.common.enums.ResultCode;
import com.elvish.efast.common.utils.CommonUtil;
import com.elvish.efast.common.utils.ResultUtil;
import com.elvish.efast.modules.sys.entity.User;
import com.elvish.efast.modules.sys.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.nutz.lang.Lang;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (TUser)表控制层
 *
 * @author makejava
 * @since 2018-11-15 14:22:06
 */
@RestController
@RequestMapping("/user")
@Api("用户管理")
@Slf4j
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService tUserService;

    /**
     * 查询角色列表
     *
     * @param page 分页信息
     * @param dict 查询参数
     * @return 响应消息
     */
    @ApiOperation(value = "查询角色列表", notes = "查询角色列表 = 分页信息 + 查询参数")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "分页信息", required = true, dataType = "Page"),
        @ApiImplicitParam(name = "user", value = "查询信息", dataType = "User")
    })
    @GetMapping("/queryAll")
    @NeedPage
    public TableResponse queryAll(Page page, User user) {
        return TableResponse.builder().data(tUserService.queryAll(user)).build();
    }

    /**
     * 编辑用户
     * @param oper 操作符
     * @param user 用户信息
     * @return 响应消息
     */
    @ApiOperation(value = "编辑用户", notes = "查询角色列表 = 操作符 + 用户信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "oper", value = "操作符", required = true, dataType = "Integer"),
        @ApiImplicitParam(name = "user", value = "用户信息", dataType = "User")
    })
    @PostMapping("/editUser")
    public Result editUser(int oper, @RequestBody User user) {
        Result result = null;
        try {
            if (oper == Const.OperationType.ADD) {
                user.setId(CommonUtil.uuid());
                this.tUserService.insert(user);
            } else if (oper == Const.OperationType.UPDATE) {
                this.tUserService.update(user);
            } else if (oper == Const.OperationType.DELETE) {
                String[] ids = user.getIds();
                if (Lang.isEmpty(ids)) {
                    ids = new String[1];
                    ids[0] = user.getId();
                }
                this.tUserService.deleteByIds(ids);
            }
            result = ResultUtil.success();
        } catch (Exception e) {
            log.error("editRole error", e);
            result = ResultUtil.fail(ResultCode.ERROR);
        }
        return result;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User selectOne(String id) {
        return this.tUserService.queryById(id);
    }

}